package com.framework.base.struts;

import java.util.HashMap;

import org.apache.log4j.Logger;

import com.framework.base.constant.ActionConstants;
import com.framework.base.service.BaseService;
import com.framework.base.taglib.page.Pagination;
import com.framework.base.util.DateUtil;
import com.framework.base.util.ParamsUrlUtil;
import com.framework.base.util.Struts2Utils;
import com.opensymphony.xwork2.ActionSupport;

/**
 * * Title: * * Description: * * Copyright: Copyright (c) 2005 * * Company: *
 * 
 * @author www.51wuxia.com
 * @version 1.0
 */

@SuppressWarnings("serial")
public class BaseAction extends ActionSupport{
	private BaseService baseService; //服务基接口
	private BaseVo vo; //域的基类对象
	private String paramsUrl; //用于进行参数转发
	private HashMap<String,Object> queryMap; //获取查询参数
	private Pagination page=new Pagination(); //分页对象
	Logger logger = Logger.getLogger(getClass()); // 作为日志的公用对象
	/**
	 * 取得日志对象
	 * 
	 * @return 返回日志对象
	 */
	public Logger getLogger() {
		
		return logger;
	}

	/**
	 * 用于准备添加数据
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toInsert() throws Exception {
		return SUCCESS;
	}

	/**
	 * 用于添加数据
	 * 
	 * @return
	 * @throws Exception
	 */
	public String doInsert() throws Exception {
		this.getVo().setCreateidf(Struts2Utils.getCurrentUser().getIdf());
		this.getVo().setDeptidf(Struts2Utils.getCurrentUserDeptId());
		this.getVo().setCreatedate(DateUtil.getFullDate_());
		String resultName = this.getBaseService().doInsert(this.getVo());
		return resultName;
	}

	/**
	 * 用于删除指定的数据
	 * 
	 * @return
	 * @throws Exception
	 */
	public String doDelete() throws Exception {
		this.setParamsUrl(ParamsUrlUtil.dealParamsUrl(Struts2Utils.getRequest()));
		String[] pks=Struts2Utils.getRequest().getParameterValues("fid");
		String resultName = this.getBaseService().doDelete(pks);
		return resultName;
	}

	/**
	 * 用于准备修改数据
	 * 
	 * @param cfg
	 *            ActionParameter
	 * @return ActionForward
	 * @throws Exception
	 */
	public String toUpdate() throws Exception {
		String pk=Struts2Utils.getRequest().getParameter("fid");
		Result Result=this.getBaseService().getById(pk);
		this.setVo(Result.getVo());
		return Result.getForwardName();
	}

	/**
	 * 用于修改指定的数据
	 * 
	 * 
	 * @param cfg
	 * @return
	 * @throws Exception
	 */
	public String doUpdate() throws Exception {
		this.getVo().setCreatedate(DateUtil.getFullDate_());
		String resultName = this.getBaseService().doUpdate(this.getVo());
		return resultName;
	}

	/**
	 * 用于查看指定的数据
	 * 
	 * 
	 * @param cfg
	 * @return
	 * @throws Exception
	 */
	public String toView() throws Exception {
		String pk=Struts2Utils.getRequest().getParameter("fid");
		Result result=this.getBaseService().getById(pk);
		this.setVo(result.getVo());
		return result.getForwardName();
	}

	/**
	 * 用于进行分页查询
	 * @return
	 * @throws Exception
	 */
	public String toList() throws Exception {
		setQueryMap(Struts2Utils.initQueryMap(getParamsUrl(), getQueryMap()));
		HashMap<String,Object> paramsMap=(HashMap<String, Object>) Struts2Utils.getParameterMap(getQueryMap(),this.getVo());
		Result result = this.getBaseService().toList(paramsMap);
		setPage(result.getPage());
		return result.getForwardName();
	}

	/**
	 * 用于进行列表重定向
	 * @return
	 * @throws Exception 
	 */
	public String reLoad() throws Exception{
		
		toList();
		return ActionConstants.SUCCESS;
	}
	
	public BaseService getBaseService() {
		return baseService;
	}

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public BaseVo getVo() {
		return vo;
	}

	public void setVo(BaseVo vo) {
		this.vo = vo;
	}

	public String getParamsUrl() {
		return paramsUrl;
	}

	public void setParamsUrl(String paramsUrl) {
		this.paramsUrl = paramsUrl;
	}

	public HashMap<String, Object> getQueryMap() {
		return queryMap;
	}

	public void setQueryMap(HashMap<String, Object> queryMap) {
		this.queryMap = queryMap;
	}

	public Pagination getPage() {
		return page;
	}

	public void setPage(Pagination page) {
		this.page = page;
	}
	
}
